package Demo5;

import java.util.Arrays;

//接口的应用
public class Test {
    public static void main1(String[] args) {
        int[] array = {1,3,5,7,4,2};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));


    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("lisi" , 10 ,60);
        students[1] = new Student("hehe" , 12 , 30);
        students[2] = new Student("haha" , 14 ,13);
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
}

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public int score;

    public Student(String name,int age,int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String toString() {
        return "Student{" +
                "name = " + name + " " +
                "age =" + age + " " +
                "score =" + score +
                "}" + "\n";
    }

    @Override
    public int compareTo(Student o) {
        if(this.name.compareTo(o.name) > 0 ) {
            return 1;
        }else if(this.name.compareTo(o.name) < 0) {
            return -1;
        }else {
            return 0;
        }
    }
}

//string也实现是comparable接口，所以可以直接调用compareto方法比较
//equals只能判断是不是相等，要判断大小必须采用compareto方法